package com.shu.redis;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.moilioncircle.redis.replicator.RedisReplicator;
import com.moilioncircle.redis.replicator.Replicator;
import com.moilioncircle.redis.replicator.cmd.Command;
import com.moilioncircle.redis.replicator.cmd.CommandListener;
import com.moilioncircle.redis.replicator.cmd.CommandName;
import com.moilioncircle.redis.replicator.cmd.CommandParser;

public class RedisReplicatorTest {

	 public static void main(String[] args) throws Exception {
	        final Replicator replicator = new RedisReplicator("redis://192.168.1.15:6379");

	        replicator.addCommandParser(CommandName.name("APPEND"), new YourAppendParser());

	        replicator.addCommandListener(new CommandListener() {
	            public void handle(Replicator replicator, Command command) {
	                if (command instanceof YourAppendParser.YourAppendCommand) {
	                    YourAppendParser.YourAppendCommand yourAppendCommand = (YourAppendParser.YourAppendCommand) command;
	                    System.out.println(yourAppendCommand.key);
	                    System.out.println(yourAppendCommand.value);
	                }
	            }
	        });
	        replicator.open();
	    }

	    public static class YourAppendParser implements CommandParser<YourAppendParser.YourAppendCommand> {

	        public YourAppendCommand parse(Object[] command) {
	            return new YourAppendCommand(new String((byte[]) command[1], UTF_8), new String((byte[]) command[2], UTF_8));
	        }

	        public static class YourAppendCommand implements Command {
	            /**
	             *
	             */
	            private static final long serialVersionUID = 1L;
	            public final String key;
	            public final String value;

	            public YourAppendCommand(String key, String value) {
	                this.key = key;
	                this.value = value;
	            }

	            @Override
	            public String toString() {
	                return "YourAppendCommand{" +
	                        "key='" + key + '\'' +
	                        ", value='" + value + '\'' +
	                        '}';
	            }
	        }
	    }
}
